package pay

import (
	"OnlineRetailers/models/Admin"
	USER "OnlineRetailers/models/User"
	"fmt"
	"github.com/astaxie/beego/orm"
	"strconv"
	"time"
)

func checkOrder(order *USER.Order) (err error) {
	o := orm.NewOrm()
	freight := []*Admin.Freight{}

	var momey float32
	for _, cart := range order.OrderCart {
		if order.Coupon != nil && order.Coupon.AllOrClassification == 0 {
			err = checkCoupon(order.Coupon.CouponId, cart.Product.ProductId)
			if err != nil {
				return fmt.Errorf("ProductNotToCoupon")
			}
		}

		err = o.Read(cart.Product)
		if err != nil {
			return err
		}
		momey += float32(cart.Num) * cart.Product.Price
	}
	if order.MoneyReal != changeFloat32(momey) {
		return fmt.Errorf("momeyErr")
	}
	if order.Coupon != nil {

		var temp []*struct{}
		sql := "select * from `w_x_user_coupons` where `w_x_user_id`=? and `coupon_id`=? and `used`=0"
		_, err = o.Raw(sql, order.User.OpenId, order.Coupon.CouponId).QueryRows(&temp)
		if err != nil {
			fmt.Println("w_x_user_coupons")
			return err
		}

		err = o.Read(order.Coupon)
		if err != nil {
			fmt.Println("Read(order.Coupon)")
			return err
		}

		if order.Coupon.Deadline < time.Now().Unix() && order.Coupon.Long == 0 {
			return fmt.Errorf("overtime")
		}
		if order.Coupon.DiscountOrSub == 0 {
			if order.Real != order.Coupon.DiscountPrice {
				return fmt.Errorf("DiscountPriceErr")
			}
		} else {
			if order.Real != changeFloat32(float32(10-order.Coupon.Discount)/10.0*order.MoneyReal) {
				//fmt.Println(float32(10-order.Coupon.Discount)/10.0*order.MoneyReal)
				//fmt.Println(order.Coupon.Discount,order.MoneyReal,order.Real,float32(10-order.Coupon.Discount)/10.0*order.MoneyReal)
				return fmt.Errorf("DiscountErr")
			}
		}

	}
	_, err = o.QueryTable("Freight").All(&freight)
	if err != nil {

		return fmt.Errorf("Freight not find")
	}

	//fmt.Println(order)
	//fmt.Println(order.MoneyReal , changeFloat32(freight[0].Limit))
	//fmt.Println(order.Freight , changeFloat32(freight[0].Money))

	if order.MoneyReal < changeFloat32(freight[0].Limit) {
		if order.Freight != changeFloat32(freight[0].Money) {
			return fmt.Errorf("order.Freight != freight[0].Money")
		}
	}
	if changeFloat32(order.Money) != changeFloat32(order.MoneyReal-order.Real+order.Freight) {
		//fmt.Println(changeFloat32(order.Money),changeFloat32(order.MoneyReal)-changeFloat32(order.Real))
		//fmt.Println(changeFloat32(23.1354314))
		return fmt.Errorf("order.Money!=order.MoneyReal-order.Real")
	}
	return nil
}

func changeFloat32(d float32) float32 {
	s := fmt.Sprintf("%0.2f", d)
	f, _ := strconv.ParseFloat(s, 32)
	return float32(f)
}

func checkCoupon(couponId, productId string) (err error) {
	o := orm.NewOrm()
	var temp []*struct{}
	class := []*Admin.Classification{}
	sql := "select * `coupon_classifications` where `coupon_id`=?"
	_, err = o.Raw(sql, couponId).QueryRows(&class)
	if err != nil {
		return err
	}
	for _, classTemp := range class {
		sql := "select * `classification_product_detaileds` where `classification_id`=? and `product_detailed_id`=?"
		_, err = o.Raw(sql, classTemp.ClassificationId).QueryRows(&temp)
		if err != nil {
			return err
		}
	}
	return nil
}
